package cn.com.header.core.util;

import cn.com.header.web.model.sys.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class JSONResult {
	private boolean success = true;
	private String msg;

	public static final String USER_IN_SESSION = "userinfo";
	private static final String VERIFYCODE_IN_SESSION = "RANDOMCODE_IN_SESSION";

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * 标志为错误
	 * @param errorMsg 错误信息
	 */
	public void mark(String errorMsg) {
		this.success = false;
		this.msg = errorMsg;
	}
	private static HttpSession getSession() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest().getSession();
	}

	public static String getSessionCode(){
		return (String)getSession().getAttribute(VERIFYCODE_IN_SESSION);
	}
	public static void setCurrentUser(User current) {
		getSession().setAttribute(USER_IN_SESSION, current);
	}

	public static User getCurrentUser() {
		return (User) getSession().getAttribute(
				JSONResult.USER_IN_SESSION);
	}
}
